package com.sf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sf.domain.*;
import com.sf.mapper.DepartmentMapper;
import com.sf.mapper.ProductMapper;
import com.sf.mapper.UserLikeMapper;
import com.sf.mapper.UserMapper;
import com.sf.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Tuple;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private UserLikeMapper likeMapper;
    @Autowired
    private DepartmentMapper mapper;
    @Override
    public Product select(String name) {
        return productMapper.selectByName(name);
    }

    @Override
    public List<Product> list() {
        Jedis jedis = jedisPool.getResource();
//        从redis当中查询数据
        List<String> list = jedis.hvals("productList");
        List<Product> list1=new ArrayList<>();
//        如果redis当中没有数据
        if (list == null || list.isEmpty()) {
            list1 = productMapper.list();
            //        查询mysql，并且把mysql 数据放到redis  当中
//            把数据放到redis当中
            for (Product product : list1) {
                jedis.hset("productList", product.getId().toString(), JSONObject.toJSONString(product));
            }
            return list1;
        }else {
         for (String s:list){
             list1.add(JSONObject.parseObject(s,Product.class));
         }
         return list1;
        }
    }

    @Override
    public Product product(Long id) {
        Jedis jedis=jedisPool.getResource();
        Product product;
        if (jedis.get("productId"+id)==null){
            product=productMapper.selectById(id);
            jedis.set("productId"+id,JSONObject.toJSONString(product));
        }else {
          product=JSONObject.parseObject(jedis.get("productId"+id),Product.class);
        }
        return product;
    }

    @Override
    public void list1() {
       List<UserLike>list=likeMapper.list();
       Jedis jedis=jedisPool.getResource();
           for (UserLike userLike : list) {
               jedis.zadd("user_like", userLike.getLike(), JSONObject.toJSONString(userLike));
           }
    }

    @Override
    public List<UserLike> list2() {
        Jedis jedis=jedisPool.getResource();
        List<UserLike>list = new ArrayList<>();
        Set<String> set;
        set=jedis.zrevrange("user_like",0,-1);
        for (String s : set) {
            list.add(JSONObject.parseObject(s,UserLike.class));
        }
        return list;
    }

//    @Override
//    public List<UserLike> updatauserLike(Long id) {
//        UserLike userLike= likeMapper.selectById(id);
//        Jedis jedis=jedisPool.getResource();
//        String userLikeJson = JSONObject.toJSONString(userLike, SerializerFeature.DisableCircularReferenceDetect);
//        System.out.println(JSONObject.toJSONString(userLike));
//        jedis.zincrby("user_like",10,userLikeJson);
//        jedis.zincrby("user_like",10,JSONObject.toJSONString(userLike));
//        List<UserLike>list = new ArrayList<>();
//        Set<String> set;
//        set=jedis.zrevrange("user_like",0,-1);
//        for (String s : set) {
//            list.add(JSONObject.parseObject(s,UserLike.class));
//        }
//        return list;
//    }
    @Override
    public List<UserLike> updatauserLike(Long id){
        UserLike userLike=likeMapper.selectById(id);
        Jedis jedis=jedisPool.getResource();
        jedis.zrem("user_like",JSONObject.toJSONString(userLike));
        likeMapper.upDataById(id);
        UserLike userLike1=likeMapper.selectById(id);
        jedis.zadd("user_like",userLike1.getLike(),JSONObject.toJSONString(userLike1));
        List<UserLike>list = new ArrayList<>();
        Set<String> set;
        set=jedis.zrevrange("user_like",0,-1);
        for (String s : set) {
            list.add(JSONObject.parseObject(s,UserLike.class));
        }
        return list;
    }

    @Override
    public List<Like> Likelist() {
        Jedis jedis=jedisPool.getResource();
        List<Like>list = new ArrayList<>();
        Set<Tuple> set;
        set=jedis.zrevrangeWithScores("userlike1",0,-1);
        for (Tuple tuple : set) {
            UserLike userLike=JSONObject.parseObject(tuple.getElement(),UserLike.class);
            Long k=JSONObject.parseObject(String.valueOf(tuple.getScore()),Long.class);
            list.add(new Like(userLike,k));
        }
        System.out.println(list);
        return list;
    }
    @Override
    public void list11() {
        List<UserLike>list=likeMapper.list1();
        Jedis jedis=jedisPool.getResource();
        for (UserLike userLike : list) {
            jedis.zadd("userlike1", userLike.getLike(), JSONObject.toJSONString(userLike));
        }
        for (UserLike userLike : list) {
            jedis.set(JSONObject.toJSONString(userLike.getId()),JSONObject.toJSONString(userLike));
        }
    }

    @Override
    public List<Like> updatauserLike1(Long id) {
        Jedis jedis=jedisPool.getResource();
        String x= String.valueOf(id);
        System.out.println(x);
        UserLike userLike=JSONObject.parseObject(jedis.get(x),UserLike.class);
        System.out.println(userLike);
        Double z=jedis.zincrby("userlike1",10,JSONObject.toJSONString(userLike));
        System.out.println(z);
        return null;
//        List<Like>list = new ArrayList<>();
//        Set<Tuple> set;
//        set=jedis.zrevrangeWithScores("userlike1",0,-1);
//        for (Tuple tuple : set) {
//            UserLike userLike1=JSONObject.parseObject(tuple.getElement(),UserLike.class);
//            Long k=JSONObject.parseObject(String.valueOf(tuple.getScore()),Long.class);
//            list.add(new Like(userLike1,k));
//        }
//        System.out.println(list);
//        return list;
    }

    @Override
    public void upDataMySql() {
        Jedis jedis=jedisPool.getResource();
        Set<Tuple> set;List<Like>list=new ArrayList<>();
        set=jedis.zrevrangeWithScores("userlike1",0,-1);
        for (Tuple tuple : set) {
            UserLike userLike=JSONObject.parseObject(tuple.getElement(),UserLike.class);
            Long like=JSONObject.parseObject(String.valueOf(tuple.getScore()),Long.class);
            list.add(new Like(userLike,like));
        }
        System.out.println(list);
        for (Like like : list) {
            if (!Objects.equals(like.getLike(), like.getUserLike().getLike())){
                UserLike userLike=JSONObject.parseObject(jedis.get(String.valueOf(like.getUserLike().getId())), UserLike.class);
                jedis.zrem("userlike1",JSONObject.toJSONString(userLike));
                jedis.del(String.valueOf(like.getUserLike().getId()));
                likeMapper.upDataByLike(like.getUserLike().getId(),like.getLike());
                UserLike like1=likeMapper.selectByIduserlike1(like.getUserLike().getId());
                System.out.println(like1);
                jedis.set(String.valueOf(like.getUserLike().getId()),JSONObject.toJSONString(like1));
                jedis.zadd("userlike1",like1.getLike(),JSONObject.toJSONString(like1));
            }
        }
    }

    @Override
    public List<Department> listDepartment() {
        return mapper.list();
    }

    @Override
    public void addDepartment( Department department) {
        mapper.addDepartment(department);
    }

    @Override
    public void updateDepartment(Department department) {
        mapper.updateById(department);
    }

    @Override
    public void deleteDepartment(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public boolean login(User user) {
        User user1=userMapper.selectByUserName(user.getUsername());
        return Objects.equals(user1.getPassword(), user.getPassword());
    }
}
